package com.gugee.tiktok.common.model.dal.mention.impl;

import com.gugee.tiktok.common.model.dal.mention.AwemeMentionExtRepository;
import com.gugee.tiktok.common.model.mysql.AwemeMention;
import com.xiguaji.boot.toolkit.DateTimeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

public class AwemeMentionExtRepositoryImpl implements AwemeMentionExtRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public void batchSaveAwemeMention(List<AwemeMention> awemeMentions){
        long seconds = DateTimeUtils.currentTimeSecond();
        /**
         * todo 收录标签，若想解耦，可将hid发送到消息队列批量消费
         */
        jdbcTemplate.batchUpdate(
                "INSERT INTO `tiktok`.`aweme_mention`" +
                        "(`id`, `aweme_id`," +
                        " `uid`, `aweme_create_time`, `mention_uid`, `mention_avatar`, `mention_nickname`,`mention_unique_id`," +
                        " `create_time`, `update_time`, `create_by`, `update_by`,`mention_custom_verify`,`mention_enterprise_verify_reason`)" +
                        " VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
                , new BatchPreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                        AwemeMention awemeMention = awemeMentions.get(i);
                        preparedStatement.setLong(1, 0);
                        preparedStatement.setLong(2, awemeMention.getAwemeId());
                        preparedStatement.setLong(3,awemeMention.getUid());
                        preparedStatement.setLong(4, awemeMention.getAwemeCreateTime());
                        preparedStatement.setLong(5, awemeMention.getMentionUid());
                        preparedStatement.setString(6, awemeMention.getMentionAvatar());
                        preparedStatement.setString(7,awemeMention.getMentionNickname());
                        preparedStatement.setString(8, awemeMention.getMentionUniqueId());
                        preparedStatement.setLong(9,seconds);
                        preparedStatement.setLong(10,seconds);
                        preparedStatement.setString(11,"batch save");
                        preparedStatement.setString(12,"batch save");
                        preparedStatement.setString(13,awemeMention.getMentionCustomVerify());
                        preparedStatement.setString(14,awemeMention.getMentionEnterpriseVerifyReason());
                    }

                    @Override
                    public int getBatchSize() {
                        return awemeMentions.size();
                    }
                });
    }

}
